import 'dart:convert' show json;

import 'package:flutter/cupertino.dart';
import 'package:mahjong/const/CacheConst.dart';
import 'package:mahjong/entity/user/VerificationMode.dart';
import 'package:mahjong/util/CacheUtil.dart';
import 'package:mahjong/util/UserUtil.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class UserEntityInfo with ChangeNotifier {
  UserEntityInfo(
      {this.sysUser,
      this.grantedAuthorities,
      this.authoritiesList,
      this.tokenAuthority,
      this.verificationMode});
  updateUserInfo({String imagePath}) {
    this.sysUser.portraitUrl = imagePath;
    UserEntityInfo userEntityInfo = UserUtil.getUserInfo();
    userEntityInfo.sysUser.portraitUrl = imagePath;
    CacheUtil.preferencesInstance
        .setString(CacheConst.userInfo, json.encode(userEntityInfo.toJson()));
    notifyListeners();
  }

  factory UserEntityInfo.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }
    final List<GrantedAuthorities> grantedAuthorities =
        jsonRes['grantedAuthorities'] is List ? <GrantedAuthorities>[] : null;
    if (grantedAuthorities != null) {
      for (final dynamic item in jsonRes['grantedAuthorities']) {
        if (item != null) {
          grantedAuthorities.add(
              GrantedAuthorities.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }

    final List<String> authoritiesList =
        jsonRes['authoritiesList'] is List ? <String>[] : null;
    if (authoritiesList != null) {
      for (final dynamic item in jsonRes['authoritiesList']) {
        if (item != null) {
          authoritiesList.add(asT<String>(item));
        }
      }
    }

    return UserEntityInfo(
        sysUser:
            SysUser.fromJson(asT<Map<String, dynamic>>(jsonRes['sysUser'])),
        grantedAuthorities: grantedAuthorities,
        authoritiesList: authoritiesList,
        tokenAuthority: asT<String>(jsonRes['tokenAuthority']),
        verificationMode: asT<VerificationMode>(jsonRes['verificationMode']));
  }

  SysUser sysUser;
  List<GrantedAuthorities> grantedAuthorities;
  List<String> authoritiesList;
  String tokenAuthority;
  VerificationMode verificationMode;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'sysUser': sysUser,
        'grantedAuthorities': grantedAuthorities,
        'authoritiesList': authoritiesList,
        'tokenAuthority': tokenAuthority,
        'verificationMode': verificationMode
      };

  @override
  String toString() {
    return json.encode(this);
  }

  void updateInfo(UserEntityInfo userInfo) {
    this.authoritiesList = userInfo.authoritiesList;
    this.sysUser = userInfo.sysUser;
    this.tokenAuthority = userInfo.tokenAuthority;
    this.grantedAuthorities = userInfo.grantedAuthorities;
    this.verificationMode = userInfo.verificationMode;
  }
}

class SysUser {
  SysUser({
    this.id,
    this.name,
    this.code,
    this.age,
    this.phone,
    this.username,
    this.password,
    this.email,
    this.portraitUrl,
    this.version,
    this.createDate,
    this.updateDate,
    this.updater,
    this.creater,
  });
  SysUser.init(this.username, this.password);

  factory SysUser.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : SysUser(
          id: asT<int>(jsonRes['id']),
          name: asT<String>(jsonRes['name']),
          code: asT<String>(jsonRes['code']),
          age: asT<int>(jsonRes['age']),
          phone: asT<String>(jsonRes['phone']),
          username: asT<String>(jsonRes['username']),
          password: asT<String>(jsonRes['password']),
          email: asT<String>(jsonRes['email']),
          portraitUrl: asT<String>(jsonRes['portraitUrl']),
          version: asT<String>(jsonRes['version']),
          createDate: asT<String>(jsonRes['createDate']),
          updateDate: asT<String>(jsonRes['updateDate']),
          updater: asT<String>(jsonRes['updater']),
          creater: asT<String>(jsonRes['creater']),
        );

  int id;
  String name;
  String code;
  int age;
  String phone;
  String username;
  String password;
  String email;
  String portraitUrl;
  String version;
  String createDate;
  String updateDate;
  String updater;
  String creater;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'name': name,
        'code': code,
        'age': age,
        'phone': phone,
        'username': username,
        'password': password,
        'email': email,
        'portraitUrl': portraitUrl,
        'version': version,
        'createDate': createDate,
        'updateDate': updateDate,
        'updater': updater,
        'creater': creater,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class GrantedAuthorities {
  GrantedAuthorities({
    this.authority,
  });

  factory GrantedAuthorities.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : GrantedAuthorities(
              authority: asT<String>(jsonRes['authority']),
            );

  String authority;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'authority': authority,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
